/**
 * 新增需求
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/',upload.array(), function(req, res, next) {
    let data = {
        isSuccess: false,
        data: null,
        msg: '新增需求失败！'
    };
    let typeCode = _.trim(String(req.body.typeCode));
    let processCode = _.trim(String(req.body.processCode));
    let corSystemsCode = _.trim(String(req.body.corSystemsCode));
    let level = _.trim(String(req.body.level));
    let content = _.trim(String(req.body.content));
    let demander = _.trim(String(req.body.demander));
    let phone = _.trim(String(req.body.phone));
    let email = _.trim(String(req.body.email));
    let solution = _.trim(String(req.body.solution));
    let recorderid = _.trim(String(req.body.recorderid));
    let recorddate = new Date().toLocaleDateString() ;
    let handlerid = _.trim(String(req.body.handlerid));
    let depart = _.trim(String(req.body.depart));
    let departid = _.trim(String(req.body.departid));
    let workshop = _.trim(String(req.body.workshop));
    let workshopid = _.trim(String(req.body.workshopid));

    let tableName = 'demand_record';
    let colName = "`typecode`, `processcode`,`corsystemscode`,`level`,`content`,`demander`,`phone`,`email`,`solution`,`handlerid`,`recorddate`,`recorderid`, `depart`, `departid`,`workshop`,`workshopid`";
    let colValue = "'" + typeCode + "','" + processCode + "','" + corSystemsCode + "','" + level + "','" + content
     + "','"+ demander + "','" + phone + "','" + email + "','" 
     + solution + "','" + handlerid + "','" + recorddate + "','" + recorderid + "','"
     + depart + "','" + departid + "','" + workshop + "','" + workshopid + "'";

    db.insert(tableName, colName, colValue, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            console.log("row" + rows.affectedRows)
            if(rows.affectedRows > 0 ) {
                data.isSuccess = true;
                data.msg = "新增需求成功！";
                return res.json(data);
            } else {
                return res.json(data);
            }
        }
    })
})

module.exports = router;